import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { ServiceRequestVO } from '#/api/cmt/service/request';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';
import { getRangePickerDefaultProps } from '#/utils';
import { getBaseCommunityOptions } from '#/api/cmt/base/community';
import { getMerchantInfoOptions } from '#/api/cmt/base/merchantInfo';
import { getServiceCategoryOptions } from '#/api/cmt/service/category';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'communityId',
      label: '社区ID',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择社区',
        api: getBaseCommunityOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'merchantId',
      label: '商户ID',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择商户',
        api: getMerchantInfoOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'requestNo',
      label: '申请编号',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入申请编号',
      },
    },
    {
      fieldName: 'categoryId',
      label: '服务分类ID',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择服务分类',
        api: getServiceCategoryOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'userId',
      label: '申请会员ID',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入申请会员ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'personId',
      label: '受益人员ID',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入受益人员ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'title',
      label: '服务标题',
      rules: 'required',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入服务标题',
      },
    },
    {
      fieldName: 'description',
      label: '服务描述',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 3,
        placeholder: '请输入服务描述',
      },
    },
    {
      fieldName: 'images',
      label: '图片',
      component: 'ImageUpload',
      formItemClass: 'col-span-2',
      componentProps: {
        multiple: true,
      },
    },
    {
      fieldName: 'videos',
      label: '视频',
      component: 'FileUpload',
      formItemClass: 'col-span-2',
      componentProps: {
        multiple: true,
      },
    },
    {
      fieldName: 'contactName',
      label: '联系人',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入联系人',
      },
    },
    {
      fieldName: 'contactPhone',
      label: '联系电话',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入联系电话',
      },
    },
    {
      fieldName: 'serviceAddress',
      label: '服务地址',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入服务地址',
      },
    },
    {
      fieldName: 'expectedDate',
      label: '期望服务日期',
      component: 'DatePicker',
      formItemClass: 'col-span-1',
      componentProps: {
        type: 'datetime',
        placeholder: '请选择期望服务日期',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
      },
    },
    {
      fieldName: 'expectedTime',
      label: '期望服务时间',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入期望服务时间',
      },
    },
    {
      fieldName: 'urgencyLevel',
      label: '紧急程度',
      component: 'Select',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择紧急程度',
        options: [
          { label: '普通', value: 1 },
          { label: '紧急', value: 2 },
          { label: '非常紧急', value: 3 },
        ],
      },
    },
    {
      fieldName: 'requestStatus',
      label: '状态',
      component: 'Select',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择状态',
        options: [
          { label: '待接单', value: 0 },
          { label: '已派单', value: 1 },
          { label: '服务中', value: 2 },
          { label: '已完成', value: 3 },
          { label: '已取消', value: 4 },
          { label: '已评价', value: 5 },
        ],
      },
    },
    {
      fieldName: 'volunteerUserId',
      label: '接单志愿者会员ID',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入接单志愿者会员ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'volunteerPersonId',
      label: '接单志愿者人员ID',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入接单志愿者人员ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'acceptTime',
      label: '接单时间',
      component: 'DatePicker',
      formItemClass: 'col-span-1',
      componentProps: {
        type: 'datetime',
        placeholder: '请选择接单时间',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
      },
    },
    {
      fieldName: 'startTime',
      label: '服务开始时间',
      component: 'DatePicker',
      formItemClass: 'col-span-1',
      componentProps: {
        type: 'datetime',
        placeholder: '请选择服务开始时间',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
      },
    },
    {
      fieldName: 'completeTime',
      label: '服务完成时间',
      component: 'DatePicker',
      formItemClass: 'col-span-1',
      componentProps: {
        type: 'datetime',
        placeholder: '请选择服务完成时间',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
      },
    },
    {
      fieldName: 'serviceDuration',
      label: '服务时长(小时)',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入服务时长',
        controlsPosition: 'right',
        precision: 2,
        class: '!w-full',
      },
    },
    {
      fieldName: 'pointReward',
      label: '积分奖励',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入积分奖励',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'experienceReward',
      label: '经验奖励',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入经验奖励',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'communityId',
      label: '社区ID',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择社区',
        api: getBaseCommunityOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'merchantId',
      label: '商户ID',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择商户',
        api: getMerchantInfoOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'categoryId',
      label: '服务分类ID',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择服务分类',
        api: getServiceCategoryOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'userId',
      label: '申请会员ID',
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入申请会员ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'urgencyLevel',
      label: '紧急程度',
      component: 'Select',
      componentProps: {
        clearable: true,
        placeholder: '请选择紧急程度',
        options: [
          { label: '普通', value: 1 },
          { label: '紧急', value: 2 },
          { label: '非常紧急', value: 3 },
        ],
      },
    },
    {
      fieldName: 'requestStatus',
      label: '状态',
      component: 'Select',
      componentProps: {
        clearable: true,
        placeholder: '请选择状态',
        options: [
          { label: '待接单', value: 0 },
          { label: '已派单', value: 1 },
          { label: '服务中', value: 2 },
          { label: '已完成', value: 3 },
          { label: '已取消', value: 4 },
          { label: '已评价', value: 5 },
        ],
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        clearable: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<ServiceRequestVO>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: 'ID',
      minWidth: 100,
    },
    {
      field: 'requestNo',
      title: '申请编号',
      minWidth: 150,
    },
    {
      field: 'communityId',
      title: '社区ID',
      minWidth: 100,
    },
    {
      field: 'merchantId',
      title: '商户ID',
      minWidth: 100,
    },
    {
      field: 'categoryId',
      title: '服务分类ID',
      minWidth: 100,
    },
    {
      field: 'userId',
      title: '申请会员ID',
      minWidth: 100,
    },
    {
      field: 'personId',
      title: '受益人员ID',
      minWidth: 100,
    },
    {
      field: 'title',
      title: '服务标题',
      minWidth: 150,
    },
    {
      field: 'description',
      title: '服务描述',
      minWidth: 200,
    },
    {
      field: 'contactName',
      title: '联系人',
      minWidth: 100,
    },
    {
      field: 'contactPhone',
      title: '联系电话',
      minWidth: 120,
    },
    {
      field: 'serviceAddress',
      title: '服务地址',
      minWidth: 150,
    },
    {
      field: 'expectedDate',
      title: '期望服务日期',
      minWidth: 150,
      formatter: 'formatDateTime',
    },
    {
      field: 'expectedTime',
      title: '期望服务时间',
      minWidth: 120,
    },
    {
      field: 'urgencyLevel',
      title: '紧急程度',
      minWidth: 100,
      slots: { default: 'urgencyLevel' },
    },
    {
      field: 'requestStatus',
      title: '状态',
      minWidth: 100,
      slots: { default: 'requestStatus' },
    },
    {
      field: 'volunteerUserId',
      title: '接单志愿者会员ID',
      minWidth: 130,
    },
    {
      field: 'volunteerPersonId',
      title: '接单志愿者人员ID',
      minWidth: 130,
    },
    {
      field: 'acceptTime',
      title: '接单时间',
      minWidth: 150,
      formatter: 'formatDateTime',
    },
    {
      field: 'startTime',
      title: '服务开始时间',
      minWidth: 150,
      formatter: 'formatDateTime',
    },
    {
      field: 'completeTime',
      title: '服务完成时间',
      minWidth: 150,
      formatter: 'formatDateTime',
    },
    {
      field: 'serviceDuration',
      title: '服务时长(小时)',
      minWidth: 120,
    },
    {
      field: 'pointReward',
      title: '积分奖励',
      minWidth: 100,
    },
    {
      field: 'experienceReward',
      title: '经验奖励',
      minWidth: 100,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 150,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
